package com.flower.service.impl;
import com.flower.entity.Catalog;

import com.flower.entity.Flower;
import com.flower.entity.FlowerDatas;
import com.flower.entity.Images;
import com.flower.mapper.FlowerMapper;
import com.flower.mapper.ImagesMapper;
import com.flower.service.FlowerService;
import com.flower.utils.PageResult;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
/**
 * FlowerServiceImpl 服务接口实现类
 * @date 2019-03-11 16:36:32
 * @version 1.0
 */
@Service
@Transactional
public class FlowerServiceImpl implements FlowerService {

	@Autowired
	private FlowerMapper flowerMapper;
	@Autowired
	private ImagesMapper imagesMapper;



	/** 添加方法 */
	public void save(FlowerDatas flowerDatas){
		try{
//			1.插入鲜花
            Flower flower = getFlower(flowerDatas);
			flowerMapper.save(flower);

            System.out.println("插入id====="+flower.getFid());

//			2.插入图片
			for (String url: flowerDatas.getUrlList()) {
				Images images=new Images();
				images.setImgurl(url);
				images.setFid(flower.getFid());
				imagesMapper.insertSelective(images);
			}



		}catch (Exception e){
			throw new RuntimeException(e);
		}

	}



    /** 修改方法 */
	public void update(FlowerDatas flowerDatas){

	    try{
//	        1.修改鲜花
            Flower flower = getFlower(flowerDatas);
            flower.setFid(flowerDatas.getFid());
            flowerMapper.update(flower);
//            2.先删除原来的图片
            Example example=new Example(Images.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("fid",flowerDatas.getFid());
            imagesMapper.deleteByExample(example);

//            3.插入图片
            for (String url: flowerDatas.getUrlList()) {
                Images images=new Images();
                images.setImgurl(url);
                images.setFid(flowerDatas.getFid());
                imagesMapper.insertSelective(images);
            }

        }catch (Exception e){
	        throw  new RuntimeException(e);
        }
	}

	/** 根据主键id删除 */
	public void delete(Serializable id){

	}

	/** 批量删除 */
	public void deleteAll(Serializable[] fid){
     try{
         //	    1.现根据fid删除图片
         Example imgExample=new Example(Images.class);
         Example.Criteria imgExampleCriteria = imgExample.createCriteria();
         imgExampleCriteria.andIn("fid",Arrays.asList(fid));
         imagesMapper.deleteByExample(imgExample);

//        2.再删除鲜花
         flowerMapper.deleteByFids(fid);
     }catch (Exception e) {
         throw new RuntimeException(e);
     }
    }

	/** 根据主键id查询 */
	public Flower findOne(Serializable id){
		return null;
	}

	/** 查询全部 */
	public List<Flower> findAll(){
		return  null;
	}

	/** 多条件分页查询 */
	public PageResult findByPage(Flower flower, int page, int rows){
		try {
			PageInfo<Flower> pageInfo = PageHelper.startPage(page, rows)
				.doSelectPageInfo(new ISelect() {
					@Override
					public void doSelect() {
						flowerMapper.findByPage();
					}
				});
			return new PageResult(pageInfo.getTotal(),pageInfo.getList());
		}catch (Exception ex){
			throw new RuntimeException(ex);
		}
	}


	/**
	 * 通过鲜花fid查询图片
	 *z
	 * @param fid
	 */
	@Override
	public List<String> findImageByFid(Long fid) {
	try{
		return  imagesMapper.findImageByFid(fid);
	}catch (Exception e){
		throw new RuntimeException(e);
	}
	}

    private Flower getFlower(FlowerDatas flowerDatas) {
        Flower flower=new Flower();
        flower.setFtitle(flowerDatas.getFtitle());
        flower.setFprice(flowerDatas.getFprice());
        flower.setFimage( flowerDatas.getUrlList().size()>0? flowerDatas.getUrlList().get(0):"");
        flower.setFsend(flowerDatas.getFsend());
        flower.setFnavi(flowerDatas.getFnavi());
        flower.setFmls(flowerDatas.getFnavi());
        flower.setFpack(flowerDatas.getFpack());
        flower.setFflorid(flowerDatas.getFflorid());
        flower.setFnumber(flowerDatas.getFnumber());
        flower.setCid(flowerDatas.getCid());
        return flower;
    }
}